package android.gpsmessenger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;

public class MessageList 
{
	private Message _sosMessage;
	private Message _helpMessage;
	private Message _message1;
	private Message _message2;

	// constructors
	MessageList()
	{
		this(new Message(""), new Message(""), new Message(""), new Message(""));
	}
	
	MessageList(Message sosMessage, Message helpMessage, Message message1, Message message2)
	{
		_sosMessage = new Message(sosMessage);
		_helpMessage = new Message(helpMessage);
		_message1 = new Message(message1);
		_message2 = new Message(message2);
	}

	MessageList(MessageList messageList)
	{
		this(messageList.getSOSMessage(), messageList.getHelpMessage(), 
				messageList.getMessage1(), messageList.getMessage2());
	}	

	// getters
	public Message getSOSMessage()
	{
		return new Message(_sosMessage);
	}
	
	public Message getHelpMessage()
	{
		return new Message(_helpMessage);
	}
	
	public Message getMessage1()
	{
		return new Message(_message1);
	}
	
	public Message getMessage2()
	{
		return new Message(_message2);
	}
	
	// setters
	public void setSOSMessage(Message sosMessage)
	{
		_sosMessage = new Message(sosMessage);
	}
	
	public void setHelpMessage(Message helpMessage)
	{
		_helpMessage = new Message(helpMessage);
	}
	
	public void setMessage1(Message message1)
	{
		_message1 = new Message(message1);
	}
	
	public void setMessage2(Message message2)
	{
		_message2 = new Message(message2);
	}
	
	// other methods
	public String toString()
	{
		return ("SOS Message: " + _sosMessage + "\n" +
				"Help Message: " + _helpMessage + "\n" +
				"Message1: " + _message1 + "\n" +
				"Message2: " + _message2 + "\n");
	}
	
	public void load(String fileName) 
	{
		Message message = new Message();
		
		String id, text;
		XmlPullParser parser = Xml.newPullParser();
		try 
		{
			File myFile = new File(fileName);
			InputStream in = new FileInputStream(myFile);
			
			parser.setInput(in, null);
			
			while (parser.getEventType() != XmlPullParser.END_DOCUMENT)
			{
				if(parser.getEventType() == XmlPullParser.START_TAG && 
		                        parser.getName().equalsIgnoreCase("message"))
				{
					id = parser.getAttributeValue(null, "id");
					text = parser.getAttributeValue(null, "text");
	
					message.setMessage(text);
					
					if (id.equals("sos"))
						setSOSMessage(message);
					else if(id.equals("help"))
						setHelpMessage(message);
					else if(id.equals("message1"))
						setMessage1(message);
					else if(id.equals("message2"))
						setMessage2(message);
				}
				parser.next();
			}
		}
		catch (IOException e) {e.printStackTrace();} 
		catch (XmlPullParserException e) {e.printStackTrace();}   
}	
		
	public void save(String fileName)
	{
		try
		{ 	
			String str = writeXML();
			File myFile = new File(fileName);
 
            myFile.createNewFile();
            FileOutputStream fOut = new FileOutputStream(myFile);
            OutputStreamWriter myOutWriter =  new OutputStreamWriter(fOut);
            myOutWriter.append(str);
            myOutWriter.close();
            fOut.close();
		}
		catch(IOException ioe) {ioe.printStackTrace();}		
	}
	
 	private String writeXML(){
 	    XmlSerializer serializer = Xml.newSerializer();
 	    StringWriter writer = new StringWriter();
 	    try {
 	        serializer.setOutput(writer);
 	        serializer.startDocument("UTF-8", true);
 	        serializer.startTag("", "messages");
 	        
 	        serializer.startTag("", "message");
 	        serializer.attribute("", "id", "sos");
 	        serializer.attribute("", "text", getSOSMessage().getMessage());
 	        serializer.endTag("", "message");
 	        
	        serializer.startTag("", "message");
 	        serializer.attribute("", "id", "help");
 	        serializer.attribute("", "text", getHelpMessage().getMessage());
 	        serializer.endTag("", "message");
 	        
	        serializer.startTag("", "message");
 	        serializer.attribute("", "id", "message1");
 	        serializer.attribute("", "text", getMessage1().getMessage());
 	        serializer.endTag("", "message");
 	         	       
	        serializer.startTag("", "message");
 	        serializer.attribute("", "id", "message2");
 	        serializer.attribute("", "text", getMessage2().getMessage());
 	        serializer.endTag("", "message");
 	        
 	        serializer.endTag("", "messages");
 	        serializer.endDocument();
 	        return writer.toString();
 	    } 
 	    catch (Exception e) {throw new RuntimeException(e);} 
 	}		
}
